package state;

import java.util.Observable;

/**
 * A class to hold the state of an individual turtle.
 * 
 * @author djs22
 *
 */
public class TurtleState extends Observable {
	
	/**
	 * the location of the turtle
	 */
	private int x;
	private int y;
	private int oldX;
	private int oldY;
	
	/**
	 * the unique id of the turtle
	 */
	private int id;
	/**
	 * the orientation of the turtle
	 */
	private int orientation;
	
	/**
	 * whether the pen is up or down
	 */
	private boolean isPenDown;
	
	/**
	 * whether the turtle is shown or not
	 */
	private boolean isShown;

	public TurtleState(int id){
		this.id=id;
		this.x=0;
		this.y=0;
		orientation = 0;
		isPenDown = true;
		isShown = true;
	}

	public void setx(int i) {
		this.oldX = this.x;
		this.x=i;
		this.setChanged();
	}
		
	public void sety(int i) {
		this.oldY = this.y;
		this.y=i;	
		this.setChanged();
	}

	public void setOrientation(int i) {
		this.orientation=i;
		this.setChanged();
	}
	
	public void setPenDown(boolean i) {
		this.isPenDown=i;
		this.setChanged();
	}
	
	public void setTurtleShown(boolean i) {
		this.isShown=i;
		this.setChanged();
	}

	public void sc() {
		this.setChanged();
	}
	
	public void setToHome() {
		this.setx(0);
		this.sety(0);
	}
	
	public int disMoved(){
		return (int) Math.pow(Math.pow(this.x-this.oldX,2)+Math.pow(this.y-this.oldY,2),.5);
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getOldX() {
		return oldX;
	}

	public void setOldX(int oldX) {
		this.oldX = oldX;
	}

	public int getOldY() {
		return oldY;
	}

	public void setOldY(int oldY) {
		this.oldY = oldY;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getOrientation() {
		return orientation;
	}

	//TODO: not sure if it is right
	public int towardPoint(int x, int y){
		return (int) Math.atan((x - this.x) / (y - this.y));
	}
	
	public int[] calculateCoordinates(int length){
		int[] result = new int[2];
		result[0]=(int) Math.rint(Math.sin(Math.toRadians(this.orientation))*length)+this.x;
		result[1]=(int) Math.rint(Math.cos(Math.toRadians(this.orientation))*length)+this.y;
		return result;

	}
	
	public boolean isPenDown(){
		return isPenDown;
	}
	
	public boolean isShown(){
		return isShown;
	}
}
